#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/6/23 11:33
# @Author  : Chenmm
# @File    : course.py
# @Software: PyCharm

from fastapi import APIRouter, Depends, Request
from sqlalchemy.orm import Session

from methods.course_method import (create_course, get_course_detail, edit_course, get_course_comment,
                                   to_comment_method, add_student_course, del_student_course,
                                   get_all_courses, get_student_like, like_method, add_like_method,
                                   delete_like_method, onsale_method)
from middlewares.mysql.database import create_db
from models.course_schema import Courses, CourseEdit, CourseCommentModel
from models.user_schema import UsernameRole
from methods.user_method import get_current_user



course_router = APIRouter()


@course_router.post("/", summary="创建课程")
def create(course: Courses, db: Session = Depends(create_db), user: UsernameRole = Depends(get_current_user)):
    return create_course(db, course, user)


@course_router.get("/", summary="获取课程详情")
def detail(course_id: int, db: Session = Depends(create_db)):
    return get_course_detail(course_id, db)


@course_router.put("/", summary="编辑课程")
def edit(
        course: CourseEdit,
        db: Session = Depends(create_db),
        user: UsernameRole = Depends(get_current_user)):
    return edit_course(course, db, user)


@course_router.get("/course_comment", summary="查看课程评论")
def course_comment(course_id: int,
        db: Session = Depends(create_db)):
    return get_course_comment(course_id, db)


@course_router.post("/course_comment", summary="发起评论")
def to_comment(comment: CourseCommentModel, user: UsernameRole = Depends(get_current_user),
               db: Session = Depends(create_db)):
    return to_comment_method(comment, user, db)


@course_router.post("/student_course", summary="加入课程")
def add(course_id: int, user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return add_student_course(course_id, user, db)


@course_router.put("/student_course", summary="退出课程")
def out(course_id: int, user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return del_student_course(course_id, user, db)


@course_router.get("/student_course/all", summary="查询所有课程")
def get_all(user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return get_all_courses(db)


@course_router.get("/student_course", summary="查询学生加入的所有课程")
def get_student_like_course(user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return get_student_like(user, db)


@course_router.get("/like", summary="获取推荐课程")
def like(db: Session = Depends(create_db)):
    return like_method(db)


@course_router.post("/like", summary="点赞课程")
async def add_like(request: Request, course_id: int, user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return await add_like_method(request, course_id, user, db)


@course_router.post("/dislike", summary="取消点赞课程")
async def delete_like(request: Request, course_id: int, user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return await delete_like_method(request, course_id, user, db)


@course_router.post("/onsale", summary="上架/下架 课程")
def onsale(course_id: int, user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return onsale_method(course_id, user, db)